package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JcCountEntity;
import com.sgb.jicai.entity.JcMyFactoryDataEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.LogUtil;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.JcMyPriceListEntity;
import com.sgb.jicai.view.fragment.JcMyPriceFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class JiCaiMyPriceModel extends ViewModel {

    private JcMyPriceFragment fragment;
    public boolean hasNext = false;//是否有下一页

    public void getList(int status, int page, int type, String keyWords) {
        Map map = new HashMap();
        map.put("limit", 10);
        map.put("page", page);
        map.put("status", status);
//        map.put("groupBuyType", type);  // 1.19版本以前用（不包含1.19版本）
        map.put("groupBuyMode", type);//1需方发起 2企业专项
        map.put("goodsName", keyWords);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
      JiCaiBaseApi.getApi().getMyPriceList(map).enqueue(new Callback<BaseEntity<JcMyFactoryDataEntity<JcMyPriceListEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<JcMyFactoryDataEntity<JcMyPriceListEntity>>> call, Response<BaseEntity<JcMyFactoryDataEntity<JcMyPriceListEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null)
                    return;
                if (null == response.body().getData())
                    return;
                if (null == response.body().getData().getRecords())
                    return;
                if (response.body().getData().getCurrent() < response.body().getData().getPages()) {
                    hasNext = true;
                } else {
                    hasNext = false;
                }
                fragment.showList(response.body().getData().getRecords());
            }

            @Override
            public void onFailure(Call<BaseEntity<JcMyFactoryDataEntity<JcMyPriceListEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void deleteData(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().deleteMyPriceList(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    fragment.initData();
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //我的报价--退出报价
    public void exitMyOffer(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "退出报价中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().exitMyOffer(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    fragment.initData();
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void getH5UrlToDetail(String id, String status, String groupbuyType) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting("mine").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?toPage=offer&no=" + id + "&status=" + status + "&groupbuyType=" + groupbuyType;
                    LogUtil.e("api", "H5链接：" + url);
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getH5Url(JcMyPriceListEntity entity) {
        String param = "";
        switch (entity.getGroupbuyType()) {//1即报集采 2汇总集采  3企业专项
            case "1":
            case "2":
            case "3":
                param = "CollectAgainPrice";
                break;
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting(param).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    fragment.showUrl(response.body().getData().getUrl() + "?no=" + entity.getNo());

                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * //刷新数量
     */
    public void getMyOfferBuyListCount(int groupBuyClass, String keyWords) {
        Map map = new HashMap();
        map.put("groupBuyMode", groupBuyClass);//1即报集采 2汇总集采 3企业专项
        map.put("goodsName", keyWords);
        JiCaiBaseApi.getApi().myOfferBuyListCountForStatus(map).enqueue((new Callback<BaseEntity<List<JcCountEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<JcCountEntity>>> call, Response<BaseEntity<List<JcCountEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    fragment.setTabNum(response.body().getData());
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<JcCountEntity>>> call, Throwable t) {

            }
        }));

    }

    //删除我的报价订单接口
    public void deletePirce(String participantNo) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "删除数据中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getdeleteMyOffer(participantNo).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(fragment.getContext(), "删除报价信息成功");
                    fragment.initData();
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void setActivity(JcMyPriceFragment jcMyPriceFragment) {
        this.fragment = jcMyPriceFragment;
    }
}
